<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="../taglibs.jsp"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-购物车</title>
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/reset.css">
	<link rel="stylesheet" type="text/css" href="${path}/static/front/css/main.css">
</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到天天生鲜!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em>张 山</em>
				</div>
				<div class="login_btn fl">
					<a href="login.html">登录</a>
					<span>|</span>
					<a href="register.html">注册</a>
				</div>
				<div class="user_link fl">
					<span>|</span>
					<a href="user_center_info.html">用户中心</a>
					<span>|</span>
					<a href="cart.html">我的购物车</a>
					<span>|</span>
					<a href="user_center_order.html">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="index.html" class="logo fl"><img src="${path}/static/front/images/logo.png"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;购物车</div>
		<div class="search_con fr">
			<input type="text" class="input_text fl" name="" placeholder="搜索商品">
			<input type="button" class="input_btn fr" name="" value="搜索">
		</div>		
	</div>

	<table id="tableId" lay-filter="tableFilter"></table>

	<div class="total_count">全部商品<em>2</em>件</div>	
	<ul class="cart_list_th clearfix">
		<li class="col01">商品名称</li>
		<li class="col02">商品单位</li>
		<li class="col03">商品价格</li>
		<li class="col04">数量</li>
		<li class="col05">小计</li>
		<li class="col06">操作</li>
	</ul>
	<c:forEach items="${list}" var="cart">
		<ul class="cart_list_td clearfix">
			<li class="col01">
				<c:if test="${cart.checked == 1}">
					<input id="checkbox${cart.id}" onclick="updateCheckStatus(${cart.id})" type="checkbox" name="selectCheckbox" checked="checked">
				</c:if>
				<c:if test="${cart.checked != 1}">
					<input id="checkbox${cart.id}" onclick="updateCheckStatus(${cart.id})" type="checkbox" name="selectCheckbox">
				</c:if>
			</li>
			<li class="col02"><img src="/pic/${cart.product.mainImage}"></li>
			<li class="col03">${cart.product.name}<br><em>25.80元/500g</em></li>
			<li class="col04">500g</li>
			<li class="col05">${cart.product.price}元</li>
			<li class="col06">
				<div class="num_add">
					<a onclick="updateQuantity(${cart.id}, 1, ${cart.product.price})" href="javascript:;" class="add fl">+</a>
					<input id="num${cart.id}" type="text" class="num_show fl" value="${cart.quantity}"/>	
					<a onclick="updateQuantitySubstract(${cart.id}, 1, ${cart.product.price})" href="javascript:;" class="minus fl">-</a>	
				</div>
			</li>
			<li id="cartItemTotalPrice${cart.id}" class="col07">${cart.product.price*cart.quantity}元</li>
			<li class="col08"><a href="javascript:void(0)" onclick="deleteById(${cart.id})">删除</a></li>
		</ul>
	</c:forEach>
	

	<ul class="settlements">
		<li class="col01"><input type="checkbox" name="" onclick="selectAll(${cart.checked})"></li>
		<li class="col02">全选</li>
		<li class="col03">合计(不含运费)：<span>¥</span><em id="totalPrice">42.60元</em><br>共计<b>2</b>件商品</li>
		<li class="col04"><a href="${path}/shipping/getAddressInfo.shtml?userId=${user.id}&checked=1">去结算</a></li>
	</ul>

	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	
	<script src="${path}/static/lib/jquery-1.11.3.js"></script>
	<script src="${path}/static/lib/mylayer.js"></script>
	<script src="${path}/static/lib/util.js"></script>
	<script src="${path}/static/lib/layui/layui.js"></script>
	
	<script type="text/javascript">
		layui.use(['layer','table'], function(){
		 	var layer = layui.layer;
		 	 var table = layui.table;
		});
		
		$(function(){
			refreshTotalPrice();
		});
		
		// 当前页面加载完了之后，计算选中商品的总价格
		function refreshTotalPrice(){
			var checkboxs = $("input[name=selectCheckbox]:checked");
			var totalPrice = 0;
			$(checkboxs).each(function(){
				// this代表当前正在遍历的checkbox的dom对象
				// checkbox的id="checkbox23"
				// this.id    checkbox23
				// substring
				var checkboxId = this.id;
				var cartId = checkboxId.substr("checkbox".length);
				var cartItemTotalPrice = $("#cartItemTotalPrice" + cartId).html();
				totalPrice += parseFloat(cartItemTotalPrice);
			});
			$("#totalPrice").html(totalPrice);
		}
		
		function updateCheckStatus(cartId){
			var checked = $("#checkbox" + cartId).prop("checked");// true/false
			if(checked){
				checked = 1;
			}else{
				checked = 0;
			}
			$.post(
				"${path}/cart/updateCheckStatus.shtml",
				{"cartId": cartId, "checked": checked},
				function(jsonObj){
					if(jsonObj.code == util.SUCCESS){
						mylayer.successMsg(jsonObj.msg);
						refreshTotalPrice();
					}else{
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			);
		}

		function deleteById(id){
			layer.confirm("您确认要删除么？",
				function(index){
				console.log(index);
					$.post(
						"${path}/cart/deleteById.shtml",
						{"id": id},
						function(jsonObj){
							if(jsonObj.code == util.SUCCESS){
								mylayer.successMsg(jsonObj.msg);
							    $("#cartItem" + id).remove();
// 							    refreshTotalPrice();
								var index = parent.layer.getFrameIndex(window.name);//先得到当前iframe层的索引
								setTimeout(function(){
									parent.layer.close(index); //再执行关闭 
									// 刷新父页面
									window.parent.location.reload();
								},1000);
							}else{
								mylayer.errorMsg(jsonObj.msg);
							}
						},
						"json"
					);
				}
			);
		}
		
		
		function updateQuantity(cartId, num, price){
			var quantity = parseInt($("#num" + cartId).val());
			if(quantity == 1 && num == -1){
					return;
			}
			$.post(
				"${path}/cart/updateQuantity.shtml",
				{"cartId": cartId, "num": num},
				function(jsonObj){
					if(jsonObj.code == util.SUCCESS){
						mylayer.successMsg(jsonObj.msg);
						// 更新显示购买数量
// 						var quantity = parseInt($("#num" + cartId).val());
						quantity += num;
						$("#num" + cartId).val(quantity);
						// 更新当前购物车中的商品总价
						var totalPrice = quantity * price;
						$("#cartItemTotalPrice" + cartId).html(totalPrice);
					}else{
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			);
		}
		
		function updateQuantitySubstract(cartId, num, price){
			var quantity = parseInt($("#num" + cartId).val());
			if(quantity == 1 && num == -1){
					return;
			}
			$.post(
				"${path}/cart/updateQuantitySubstract.shtml",
				{"cartId": cartId, "num": num},
				function(jsonObj){
					if(jsonObj.code == util.SUCCESS){
						mylayer.successMsg(jsonObj.msg);
						// 更新显示购买数量
						
						quantity -= num;
 						
						$("#num" + cartId).val(quantity);
						// 更新当前购物车中的商品总价
						var totalPrice = quantity * price;
						$("#cartItemTotalPrice" + cartId).html(totalPrice);
					}else{
						mylayer.errorMsg(jsonObj.msg);
					}
				}
			);
		}
		
// 		 table.on('toolbar(tableFilter)', function(obj){
// 			    var checkStatus = table.checkStatus(obj.config.id);
// 			    switch(obj.event){
// 			      case 'selectAll':
// 			        var data = checkStatus.data;
// 			       	var ids = [];// [1,2,3]
// 			       	for (var i in data) {
// 			       		ids.push(data[i].id);
// 			       	}
// 			       	ids = ids.join(','); // '1,2,3'
// 			       	layer.confirm('真的全选么', function(index){
// 				    	  $.ajax({
// 				    		  url : "${path}/cart/selectAll.shtml",
// 				    		  data : {"ids" : ids},
// 				    		  dataType : "json",
// 				    		  type : "post",
// 				    		  success : function(jsonObj) {
// 				    			  if (jsonObj.code == util.SUCCESS) {
// 				    				  mylayer.successMsg(jsonObj.msg);
// 				    				  // 重新加载数据表格
// 				    				  table.reload("tableReload");
// 				    			  } else {
// 									  mylayer.errorMsg(jsonObj.msg);		    				  
// 				    			  }
// 				    		  }
// 				    	  });
// 				          layer.close(index);
// 				      });
// 			      break;
			          
// 			    };
// 			  });
		
		function selectAll(checked){
			$.post(
				"${path}/cart/selectAll.shtml",
				{"checked": checked},
				function(jsonObj){
					if(jsonObj.code == util.SUCCESS){
// 						mylayer.successMsg(jsonObj.msg);
						refreshTotalPrice();
						location.reload();
					}else{
// 						mylayer.errorMsg(jsonObj.msg);
						location.reload();
					}
				}
			);
		}
		
	</script>
	
</body>
</html>